﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Auction_Server.Business;
using System.Windows.Threading;
using System.Data;
using WaterKit.TcpUdp;
using System.Threading;
using System.Net;
using WaterKit.Base;

namespace Auction_Server
{
    public partial class AppWindow : Window
    {
        /// <summary>
        /// 当前时间定时器
        /// </summary>
        DispatcherTimer currentTimer = new DispatcherTimer();
        /// <summary>
        /// 标的开始倒计时定时器
        /// </summary>
        DispatcherTimer startTargetTimer = new DispatcherTimer();
        /// <summary>
        /// 标的结束倒计时定时器
        /// </summary>
        DispatcherTimer endTargetTimer = new DispatcherTimer();
        /// <summary>
        /// 确认标的底价倒计时定时器
        /// </summary>
        DispatcherTimer confirmTimer = new DispatcherTimer();

        /// <summary>
        /// 竞拍记录
        /// </summary>
        DataTable bidPriceDt = new DataTable();

        /// <summary>
        /// 定时器使用时间
        /// </summary>
        DateTime eventTime = new DateTime();

        /// <summary>
        /// 向数据库写入最高价
        /// </summary>
        baseDataService setBidPriceFunc = new baseDataService();

        /// <summary>
        /// 初始化 App
        /// </summary>
        public AppWindow()
        {
            //绑定服务器接收事件
            App.MySer.RecvData += new NetEvent(this.RecvData);
            //客户端关闭
            App.MySer.ClientClose += new NetEvent(this.ClientClose);

            //初始化界面组件
            InitializeComponent();

            //
            bidData_Init();
        }

        /// <summary>
        /// 初始化竞拍流程数据
        /// </summary>
        void bidData_Init()
        {
            //定义竞拍记录表
            bidPriceDt.Columns.Add("companyName", typeof(string));
            bidPriceDt.Columns.Add("Time", typeof(string));
            bidPriceDt.Columns.Add("maxMoney", typeof(string));
            bidPriceDt.Columns.Add("bidMoney", typeof(string));
            bidPriceDt.Columns.Add("Result", typeof(string));

            //拍卖会信息
            lblAuctionName.Content = stringOpr.GetSubString(App.todayAuction.Rows[0]["auction_name"].ToString(), 20, "...");
            lblAuctionName.ToolTip = App.todayAuction.Rows[0]["auction_name"].ToString();
            lblAuctionTime.Content = Convert.ToDateTime(App.todayAuction.Rows[0]["auction_date"]).ToString("yyyy-MM-dd");
            lblAuctionAddr.Content = stringOpr.GetSubString(App.todayAuction.Rows[0]["auction_add"].ToString(), 20, "...");
            lblAuctionAddr.ToolTip = App.todayAuction.Rows[0]["auction_add"].ToString();

            //定时器绑定
            currentTimer.Tick += new EventHandler(currentTimer_Tick);
            currentTimer.Interval = new TimeSpan(0, 0, 1);
            currentTimer.Start();

            startTargetTimer.Tick += new EventHandler(startTargetTimer_Tick);
            startTargetTimer.Interval = new TimeSpan(0, 0, 1);

            endTargetTimer.Tick += new EventHandler(endTargetTimer_Tick);
            endTargetTimer.Interval = new TimeSpan(0, 0, 1);

            confirmTimer.Tick += new EventHandler(confirmTimer_Tick);
            confirmTimer.Interval = new TimeSpan(0, 0, 1);

            //标的列表
            targetDG_Init();
        }

        /// <summary>
        /// 初始化拍卖会下标的列表
        /// </summary>
        void targetDG_Init()
        {
            auctionAction auction = new auctionAction();
            dgTarget.DataContext = auction.GetAuctionTargets(App.currentAuctionNo);
            dgTarget.SetBinding(ListView.ItemsSourceProperty, new Binding());
        }

        /// <summary>
        /// 清除标的信息,标的竞拍结束后调用
        /// </summary>
        void target_clear()
        {
            App.clientCanLogin = false;

            App.currentTargetCompany.Clear();

            App.currentTargetNo = "";

            App.currentTarget = null;

            App.userTargetStr = "";

            App.maxMoneyBidUser = "";

            App.maxMoney = 0;

            App.htBidCodeToName.Clear();

            App.htTerminal.Clear();
        }

        /// <summary>
        /// 开始标的拍卖
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStartTarget_Click(object sender, RoutedEventArgs e)
        {
            //开始标的流程,1、竞拍倒计时(接受客户登录),2、竞拍开始,3、竞拍结束：(1)流拍 (2)成功--更新标的、商品状态,打印最高报价确认书

            App.currentTarget = (sender as Button).Tag as targetModel;

            App.maxMoney = double.Parse(App.currentTarget.Product_PRICE);

            //页面控件赋值
            lblTargetName.Content = App.currentTarget.Target_NAME;
            lblAuctioneer.Content = App.currentTarget.Auctioneer_NAME;
            lblAuctionType.Content = App.htBidCodeToName[App.currentTarget.Auction_TYPE];
            lblIncrease.Content = App.currentTarget.Increment_MONEY.ToString("#,###") + "元";
            lblProductName.Content = App.currentTarget.Product_NAME;
            lblProductLv.Content = App.htBidCodeToName[App.currentTarget.Product_Lv];
            lblProductPrice.Content = double.Parse(App.currentTarget.Product_PRICE).ToString("#,###") + "元";
            lblProductNum.Content = App.currentTarget.Product_AUCTION_METE + App.htBidCodeToName[App.currentTarget.Product_MEASUREMENT];

            //商品图片
            System.Windows.Media.Imaging.BitmapImage image_uri = new System.Windows.Media.Imaging.BitmapImage();
            image_uri.BeginInit();
            image_uri.UriSource = new Uri(App.currentTarget.Product_PIC, UriKind.RelativeOrAbsolute);
            image_uri.EndInit();
            imgProductPic.Source = image_uri;

            //重置标的列表状态
            List<targetModel> dtSource = (List<targetModel>)dgTarget.ItemsSource;
            for (int i = 0; i < dtSource.Count; i++)
            {
                if (dtSource[i].Target_NO == App.currentTarget.Target_NO)
                {
                    dtSource[i].Status = "开拍倒计时";
                }
                dtSource[i].BtnCaption = "禁用";
                dtSource[i].BtnEnable = false;
            }
            dgTarget.ItemsSource = null;
            dgTarget.ItemsSource = dtSource;

            //绑定标的竞拍用户
            targetService tarS = new targetService();
            App.currentTargetCompany = tarS.getTargetCompany(App.currentTarget.Target_NO);
            userLV.DataContext = App.currentTargetCompany;
            userLV.SetBinding(ListView.ItemsSourceProperty, new Binding());

            //界面初始化
            boxFailTarget.Visibility = Visibility.Collapsed;
            boxSuccessTarget.Visibility = Visibility.Collapsed;
            btnTargetArea.IsEnabled = true;
            areaTarget.Visibility = Visibility.Visible;
            areaTargetList.Visibility = Visibility.Collapsed;
            boxStartTarget.Visibility = Visibility.Visible;

            //用户可以登录
            App.clientCanLogin = true;

            //启动定时器
            Thread.Sleep(1000 - DateTime.Now.Millisecond);

            eventTime = DateTime.Now.AddSeconds(App.countdownTime);

            //组织客户端需要标的信息部分
            StringBuilder sbRetInfo = new StringBuilder();
            //商品名称1
            sbRetInfo.Append(App.currentTarget.Product_NAME).Append("|");
            //商品类别2
            sbRetInfo.Append(App.htBidCodeToName[App.currentTarget.Bid_TYPE]).Append("|");
            //商品图片3
            sbRetInfo.Append(App.currentTarget.Product_PIC).Append("|");
            //拍卖低价4
            sbRetInfo.Append(App.currentTarget.Product_PRICE).Append("|");
            //拍卖数量5
            sbRetInfo.Append(App.currentTarget.Product_AUCTION_METE).Append(App.htBidCodeToName[App.currentTarget.Product_MEASUREMENT]).Append("|");
            //标的名称6
            sbRetInfo.Append(App.currentTarget.Target_NAME).Append("|");
            //拍卖会开始时间7
            sbRetInfo.Append(eventTime.ToString()).Append("|");
            //拍卖类别8
            sbRetInfo.Append(App.htBidCodeToName[App.currentTarget.Bid_TYPE]).Append("|");
            //拍卖师9
            sbRetInfo.Append(App.currentTarget.Auctioneer_NAME).Append("|");
            //拍卖方式10
            sbRetInfo.Append(App.currentTarget.Auction_TYPE).Append("|");
            //竞拍时限(秒）11
            sbRetInfo.Append(App.currentTarget.AuctionTime).Append("|");
            //递增金额12
            sbRetInfo.Append(App.currentTarget.Increment_MONEY).Append("|");
            //标的当前最高价13
            sbRetInfo.Append(App.maxMoney).Append("|");
            App.userTargetStr = sbRetInfo.ToString();

            startTargetTimer.Start();
        }

        /// <summary>
        /// 竞拍结束处理事件
        /// </summary>
        void endTarget()
        {
            auctionAction auctionAction = new auctionAction();
            if ((App.maxMoney < double.Parse(App.currentTarget.Product_PRICE)) && App.isConfirmprice == false)
            {
                //流拍处理
                auctionAction.updateTarget(App.currentTarget.Target_NO, "", "", App.currentTarget.Product_NO);
                boxFailTarget.Visibility = Visibility.Visible;
            }
            else
            {
                //竞拍成功
                auctionAction.updateTarget(App.currentTarget.Target_NO, App.maxMoneyBidUser, App.maxMoney.ToString(), App.currentTarget.Product_NO);

                DataRow drUnit = App.currentTargetCompany.Select("BIDUSER_NO='" + App.maxMoneyBidUser + "'")[0];

                lblBidUser.Content = drUnit["COMPANY_NAME"].ToString();

                //金额
                string money = App.maxMoney.ToString("#,###");
                areaMaxMoney.Children.Clear();
                for (int i = 0; i < money.Length; i++)
                {
                    var img = new Image { };
                    img.SetResourceReference(Image.StyleProperty, "money_" + money[i].ToString());
                    areaMaxMoney.Children.Add(img);
                }
                var imga = new Image { };
                imga.SetResourceReference(Image.StyleProperty, "money_y");
                areaMaxMoney.Children.Add(imga);

                boxSuccessTarget.Visibility = Visibility.Visible;

                //最高报价确认书显示
                ConfirmationWindow confir = new ConfirmationWindow(App.currentTarget);
                confir.ShowDialog();
            }

            //绑定标的列表
            targetDG_Init();

            //更新拍卖会状态
            auctionAction.updateAuction(App.currentTarget.Auction_NO);
        }

        /// <summary>
        /// 标的开始倒计时事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void startTargetTimer_Tick(object sender, EventArgs e)
        {
            TimeSpan span = eventTime - DateTime.Now;
            if (span.TotalSeconds >= 0)
            {
                string time = new TimeSpan(0, 0, Convert.ToInt32(span.TotalSeconds)).ToString();
                areaStartClock.Children.Clear();
                for (int i = 0; i < time.Length; i++)
                {
                    var img = new Image { Width = 40 };
                    img.SetResourceReference(Image.StyleProperty, "num_" + time[i].ToString());
                    areaStartClock.Children.Add(img);
                }

                //为了避免误差,前一秒停止登录
                if (span.TotalSeconds < 2)
                {
                    //客户端不可以登录
                    App.clientCanLogin = false;
                }
            }
            else
            {
                startTargetTimer.Stop();
                //客户端不可以登录
                App.clientCanLogin = false;
                //计算确认底价倒计时
                eventTime = DateTime.Now.AddSeconds(App.confirmPriceTime);

                //显示控件
                boxStartTarget.Visibility = Visibility.Collapsed;
                boxConfirmTarget.Visibility = Visibility.Visible;

                confirmTimer.Start();
            }
        }

        /// <summary>
        /// 标的结束倒计时事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void endTargetTimer_Tick(object sender, EventArgs e)
        {
            TimeSpan span = eventTime - DateTime.Now;
            if (span.TotalMilliseconds > 0)
            {
                string time = new TimeSpan(0, 0, Convert.ToInt32(span.TotalSeconds)).ToString();
                areaEndClock.Children.Clear();
                for (int i = 0; i < time.Length; i++)
                {
                    var img = new Image { Height = 30, Width = 30 };
                    img.SetResourceReference(Image.StyleProperty, "num_" + time[i].ToString());
                    areaEndClock.Children.Add(img);
                }
            }
            else
            {
                //延时拍卖
                if (App.currentTarget.Auction_TYPE == "AUCTION_TYPE02")
                {
                    if (App.canCycle == true)
                    {
                        eventTime = DateTime.Now.AddSeconds(App.currentTarget.AuctionTime);
                        App.canCycle = false;
                    }
                    else
                    {
                        //客户端不可以竞拍
                        App.clientCanBid = false;
                        endTargetTimer.Stop();
                        boxEndTarget.Visibility = Visibility.Collapsed;
                        //标的处理
                        endTarget();
                    }
                }
                //限时拍卖
                else
                {
                    //客户端不可以竞拍
                    App.clientCanBid = false;
                    endTargetTimer.Stop();
                    boxEndTarget.Visibility = Visibility.Collapsed;
                    //标的处理
                    endTarget();
                }
            }
        }

        /// <summary>
        /// 标的确认底价事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void confirmTimer_Tick(object sender, EventArgs e)
        {
            TimeSpan span = eventTime - DateTime.Now;

            if (span.TotalSeconds >= 0)
            {
                string time = new TimeSpan(0, 0, Convert.ToInt32(span.TotalSeconds)).ToString();
                areaConfirmClock.Children.Clear();
                for (int i = 0; i < time.Length; i++)
                {
                    var img = new Image { Width = 40 };
                    img.SetResourceReference(Image.StyleProperty, "num_" + time[i].ToString());
                    areaConfirmClock.Children.Add(img);
                }
            }
            else
            {
                confirmTimer.Stop();

                //启动结束定时器
                eventTime = DateTime.Now.AddSeconds(App.currentTarget.AuctionTime);
                endTargetTimer.Start();

                //显示控件
                boxConfirmTarget.Visibility = Visibility.Collapsed;
                boxEndTarget.Visibility = Visibility.Visible;

                //显示当前价格
                areaCurrentMoney.Children.Clear();
                string money = App.maxMoney.ToString("#,###");
                for (int i = 0; i < money.Length; i++)
                {
                    var img = new Image { };
                    img.SetResourceReference(Image.StyleProperty, "money_" + money[i].ToString());
                    areaCurrentMoney.Children.Add(img);
                }
                var imga = new Image { };
                imga.SetResourceReference(Image.StyleProperty, "money_y");
                areaCurrentMoney.Children.Add(imga);
            }
        }

        /// <summary>
        /// 当前时间计时器事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void currentTimer_Tick(object sender, EventArgs e)
        {
            //
        }

        /// <summary>
        /// 确认底价
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="bidTime"></param>
        /// <param name="status"></param>
        void confirmPrice(string ip, DateTime bidTime, bool status = true)
        {
            //获取竞价单位
            DataRow drUnit = App.currentTargetCompany.Select("SEATNUMBER='" + App.htTerminal[ip] + "'")[0];

            //竞价成功
            if (status)
            {
                bidPriceDt.Rows.Add(drUnit["COMPANY_NAME"].ToString(), bidTime.ToString("HH:mm:ss fff"), 0, 0, "确认底价");
            }

            //显示竞价列表
            this.Dispatcher.Invoke(new Action(() =>
            {
                DataView bidPriceDV = new DataView(bidPriceDt);
                bidPriceDV.Sort = "bidMoney desc";
                bidPriceLV.DataContext = bidPriceDV.ToTable();
                bidPriceLV.SetBinding(ListView.ItemsSourceProperty, new Binding());
            }));
        }

        /// <summary>
        /// 执行竞价页面记录
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="bidTime"></param>
        /// <param name="maxMoney"></param>
        /// <param name="status"></param>
        void setBidPrice(string ip, DateTime bidTime, double maxMoney, bool status)
        {
            //获取竞价单位
            DataRow drUnit = App.currentTargetCompany.Select("SEATNUMBER='" + App.htTerminal[ip] + "'")[0];

            //竞价成功
            if (status)
            {
                //全局竞拍数据更新
                App.maxMoney = maxMoney;
                App.maxMoneyBidUser = drUnit["BIDUSER_NO"].ToString();

                //延时拍卖
                if (App.currentTarget.Auction_TYPE == "AUCTION_TYPE02")
                {
                    eventTime = DateTime.Now.AddSeconds(App.currentTarget.AuctionTime);
                }

                //修改页面实时显示部分
                this.Dispatcher.Invoke(new Action(() =>
                {
                    areaCurrentMoney.Children.Clear();
                    string money = App.maxMoney.ToString("#,###");
                    for (int i = 0; i < money.Length; i++)
                    {
                        var img = new Image { };
                        img.SetResourceReference(Image.StyleProperty, "money_" + money[i].ToString());
                        areaCurrentMoney.Children.Add(img);
                    }
                    var imga = new Image { };
                    imga.SetResourceReference(Image.StyleProperty, "money_y");
                    areaCurrentMoney.Children.Add(imga);
                }));
                setBidPriceFunc.saveBidRecord(App.currentTarget.Target_NO, maxMoney, drUnit["BIDUSER_NO"].ToString(), bidTime);

                bidPriceDt.Rows.Add(drUnit["COMPANY_NAME"].ToString(), bidTime.ToString("HH:mm:ss fff"), maxMoney, App.maxMoney, "竞价成功");
            }
            //竞价失败
            else
            {
                bidPriceDt.Rows.Add(drUnit["COMPANY_NAME"].ToString(), bidTime.ToString("HH:mm:ss fff"), maxMoney, App.maxMoney, "竞价失败");
            }

            //线程更新竞价记录
            this.Dispatcher.Invoke(new Action(() =>
            {
                DataView bidPriceDV = new DataView(bidPriceDt);
                bidPriceDV.Sort = "bidMoney desc";
                bidPriceLV.DataContext = bidPriceDV.ToTable();
                bidPriceLV.SetBinding(ListView.ItemsSourceProperty, new Binding());
            }));
        }

        /// <summary>
        /// 线程更新单位状态
        /// </summary>
        /// <param name="ip"></param>
        void setClientStatus(string ip)
        {
            //取得座位号
            string seatNum = App.htTerminal[ip].ToString();
            this.Dispatcher.Invoke(new Action(() =>
            {
                int n = 0;
                foreach (DataRowView item in userLV.Items)
                {
                    if (item.Row["SEATNUMBER"].ToString() == seatNum)
                    {
                        try
                        {
                            UIElement u = userLV.ItemContainerGenerator.ContainerFromIndex(n) as UIElement;
                            u = VisualTreeHelper.GetChild(u, 0) as UIElement;
                            u = VisualTreeHelper.GetChild(u, 0) as UIElement;
                            Image f = VisualTreeHelper.GetChild(VisualTreeHelper.GetChild(u, 1), 0) as Image;
                            f.SetResourceReference(Image.StyleProperty, "isConnect");
                        }
                        catch (Exception ex)
                        {
                            UIElement u = userLV.ItemContainerGenerator.ContainerFromIndex(n) as UIElement;
                            u = VisualTreeHelper.GetChild(u, 0) as UIElement;
                            u = VisualTreeHelper.GetChild(u, 0) as UIElement;
                            u = VisualTreeHelper.GetChild(u, 0) as UIElement;
                            u = VisualTreeHelper.GetChild(u, 1) as UIElement;
                            Image f = VisualTreeHelper.GetChild(VisualTreeHelper.GetChild(u, 1), 0) as Image;
                            f.SetResourceReference(Image.StyleProperty, "isConnect");
                        }
                    }
                    n++;
                }
            }));
        }

        /// <summary>
        /// 关闭客户端
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void ClientClose(object sender, NetEventArgs e)
        {
            string IP = IPAddress.Parse(((IPEndPoint)e.Client.ClientSocket.RemoteEndPoint).Address.ToString()).ToString();
            string seatNum = App.htTerminal[IP].ToString();
            this.Dispatcher.Invoke(new Action(() =>
            {
                int n = 0;
                foreach (DataRowView item in userLV.Items)
                {
                    if (item.Row["SEATNUMBER"].ToString() == seatNum)
                    {
                        try
                        {
                            UIElement u = userLV.ItemContainerGenerator.ContainerFromIndex(n) as UIElement;
                            u = VisualTreeHelper.GetChild(u, 0) as UIElement;
                            u = VisualTreeHelper.GetChild(u, 0) as UIElement;
                            Image f = VisualTreeHelper.GetChild(VisualTreeHelper.GetChild(u, 1), 0) as Image;
                            f.SetResourceReference(Image.StyleProperty, "notConnect");
                        }
                        catch (Exception ex)
                        {
                            UIElement u = userLV.ItemContainerGenerator.ContainerFromIndex(n) as UIElement;
                            u = VisualTreeHelper.GetChild(u, 0) as UIElement;
                            u = VisualTreeHelper.GetChild(u, 0) as UIElement;
                            u = VisualTreeHelper.GetChild(u, 0) as UIElement;
                            u = VisualTreeHelper.GetChild(u, 1) as UIElement;
                            Image f = VisualTreeHelper.GetChild(VisualTreeHelper.GetChild(u, 1), 0) as Image;
                            f.SetResourceReference(Image.StyleProperty, "notConnect");
                        }
                    }
                    n++;
                }
            }));
        }

        /// <summary>
        /// 打印最高报价确认书
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPrintConfirm_Click(object sender, RoutedEventArgs e)
        {
            targetModel target = (sender as Button).Tag as targetModel;

            ConfirmationWindow confir = new ConfirmationWindow(target, false);
            confir.ShowDialog();
        }

        /// <summary>
        /// 返回拍卖会选择画面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBackAuction_Click(object sender, RoutedEventArgs e)
        {
            if (App.todayAuction.Rows.Count > 1)
            {
                StartWindow Select = new StartWindow();
                Select.Show();
                this.Close();
            }
            else
            {
                DialogWindow dialog = new DialogWindow(DialogWindow.MsgType.error, "提示信息", "今日拍卖会数量为一,列表页面无法打开");
                dialog.ShowDialog();
            }
        }

        /// <summary>
        /// 返回拍卖会所有标的列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnTargetList_Click(object sender, RoutedEventArgs e)
        {
            areaTarget.Visibility = Visibility.Collapsed;
            areaTargetList.Visibility = Visibility.Visible;
        }

        /// <summary>
        /// 返回当前正在进行竞拍界面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnTargetArea_Click(object sender, RoutedEventArgs e)
        {
            areaTarget.Visibility = Visibility.Visible;
            areaTargetList.Visibility = Visibility.Collapsed;
        }

        /// <summary>
        /// 最小化应用
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnMinApp_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }

        /// <summary>
        /// 关闭应用
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCloseApp_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }

        /// <summary>
        /// 窗体拖动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left) this.DragMove();
        }

        /// <summary>
        /// 接收数据方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void RecvData(object sender, NetEventArgs e)
        {
            //获取请求客户端IP
            string IP = IPAddress.Parse(((IPEndPoint)e.Client.ClientSocket.RemoteEndPoint).Address.ToString()).ToString();
            //去除结尾标记
            string reciveData = e.Client.Datagram.Substring(0, e.Client.Datagram.Length - 2);
            //获取请求内容
            string Data = reciveData.Substring(3, reciveData.Length - 3);
            //命令类型
            App.cmdType cmd_type = (App.cmdType)Enum.Parse(typeof(App.cmdType), reciveData.Substring(0, 3).ToString(), true);
            //登录操作
            if (cmd_type == App.cmdType.c01)
            {
                //系统是否接收登录
                if (App.clientCanLogin)
                {
                    //座位号
                    string seatNum = "";

                    if (App.htTerminal.ContainsKey(IP))
                    {
                        seatNum = App.htTerminal[IP].ToString();
                    }
                    else
                    {
                        App.sendData(e.Client, App.cmdType.c02, "暂无该终端号！");
                        return;
                    }

                    baseDataService baseData = new baseDataService();
                    string sRetMsg = baseData.userLogin(Data, seatNum, App.currentTarget.Target_NO);
                    //登录成功
                    if (sRetMsg.Trim().Length == 0)
                    {
                        //竞拍单位信息
                        DataRow drCompany = App.currentTargetCompany.Select("SEATNUMBER='" + seatNum + "'")[0];

                        StringBuilder sbRetInfo = new StringBuilder();
                        //竞拍单位编号
                        sbRetInfo.Append(drCompany["BIDUSER_NO"]).Append("|");
                        //竞拍单位名称
                        sbRetInfo.Append(drCompany["COMPANY_NAME"]).Append("|");
                        //座位号
                        sbRetInfo.Append(drCompany["SEATNUMBER"]).Append("|");
                        //委托人
                        sbRetInfo.Append(drCompany["AUCTION_USERNAME"]);

                        //返回给客户端登录信息
                        App.sendData(e.Client, App.cmdType.c03, App.userTargetStr + sbRetInfo.ToString());

                        //更新客户端状态
                        setClientStatus(IP);
                    }
                    //登录失败
                    else
                    {
                        App.sendData(e.Client, App.cmdType.c02, sRetMsg);
                    }
                }
                else
                {
                    App.sendData(e.Client, App.cmdType.c02, "系统当前不接受登录！");
                }
            }
            //竞价操作
            else if (cmd_type == App.cmdType.c04)
            {
                //检查系统是否接收竞价
                if (App.clientCanBid)
                {
                    //延时拍卖
                    if (App.currentTarget.Auction_TYPE == "AUCTION_TYPE02")
                    {
                        if (double.Parse(Data) > App.maxMoney)
                        {
                            //是否延时
                            App.canCycle = true;

                            //竞价成功
                            DataRow drUnit = App.currentTargetCompany.Select("SEATNUMBER='" + App.htTerminal[IP] + "'")[0];
                            App.maxMoneyBidUser = drUnit["BIDUSER_NO"].ToString();
                            App.maxMoney = double.Parse(Data);
                            App.auctionType2Money = App.maxMoney + App.currentTarget.Increment_MONEY;
                            setBidPrice(IP, DateTime.Now, App.maxMoney, true);
                            setBidPriceFunc.saveBidRecord(App.currentTarget.Target_NO, App.maxMoney, drUnit["BIDUSER_NO"].ToString(), DateTime.Now);
                            foreach (System.Collections.DictionaryEntry item in App.MySer.SessionTable)
                            {
                                //所有客户端更新最高价
                                App.sendData((Session)item.Value, App.cmdType.c05, drUnit["BIDUSER_NO"].ToString() + "|" + App.maxMoney.ToString());
                            }
                        }
                        else
                        {
                            //竞价失败
                            App.sendData(e.Client, App.cmdType.c06, App.maxMoney.ToString());
                            setBidPrice(IP, DateTime.Now, App.maxMoney, false);
                        }
                    }
                    //限时拍卖计算
                    else
                    {
                        if (double.Parse(Data) > App.maxMoney)
                        {
                            //竞价成功
                            setBidPrice(IP, DateTime.Now, double.Parse(Data), true);
                            //更新最高价
                            App.maxMoney = double.Parse(Data);
                            DataRow drUnit = App.currentTargetCompany.Select("SEATNUMBER='" + App.htTerminal[IP] + "'")[0];
                            foreach (System.Collections.DictionaryEntry item in App.MySer.SessionTable)
                            {
                                //所有客户端更新最高价
                                App.sendData((Session)item.Value, App.cmdType.c05, drUnit["BIDUSER_NO"].ToString() + "|" + App.maxMoney.ToString());
                            }
                        }
                        else
                        {
                            //竞价失败
                            App.sendData(e.Client, App.cmdType.c06, App.maxMoney.ToString());
                            setBidPrice(IP, DateTime.Now, App.maxMoney, false);
                        }
                    }
                }
            }
            //底价确认
            else if (cmd_type == App.cmdType.c07)
            {
                //检查系统是否接收竞价
                if (App.clientCanBid)
                {
                    //判断底价是否已经被确认
                    if (App.isConfirmprice == false)
                    {
                        App.isConfirmprice = true;
                        DataRow drUnit = App.currentTargetCompany.Select("SEATNUMBER='" + App.htTerminal[IP] + "'")[0];
                        App.maxMoneyBidUser = drUnit["BIDUSER_NO"].ToString();
                        //第一个确认底价
                        setBidPriceFunc.saveBidRecord(App.currentTarget.Target_NO, App.maxMoney, drUnit["BIDUSER_NO"].ToString(), DateTime.Now);
                    }

                    //页面显示
                    confirmPrice(IP, DateTime.Now);
                }
            }
            else
            {
                //无效命令类型
            }
        }

        /// <summary>
        /// 弹出竞拍单位信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void userLV_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            DataRowView dataRow = (DataRowView)this.userLV.SelectedItem;

            if (dataRow == null)
            {
                MessageBox.Show("请选择竞拍单位！");
            }
            else
            {
                //弹出确认单位页面
                string bidUserNo = dataRow.Row["BIDUSER_NO"].ToString();

                BidUserWindow userWindow = new BidUserWindow(bidUserNo);
                userWindow.ShowDialog();
            }
        }
    }
}
